import os
import ctypes
from ctypes import *

so_path = os.path.abspath(".")
so_path = os.path.join(so_path, "koukou.so")
so = ctypes.CDLL(so_path)

print("\n=============================")
result = so.sub_number(1,2)
print(f"整型数据直接相减 result　is: {result}")

print("=============指针操作================")
x = c_int(2)
y = c_int(3)
add_point = so.point(byref(x), byref(y))
print(f"指针操作求和result is {add_point}")


print("=============================\n")
x = c_int(2)
y = c_int(3)
print(f"交换之前　x is {x.value}, 交换之前　y is {y.value}")
add_point = so.swap(byref(x), byref(y))
print(f"交换之后　x is {x.value}, 交换之后　y is {y.value}")
print(f"交换之后　x 输出is {x}, 交换之后　y 输出is {y}")

print("\n============================")
str_char01 = "123"
str_char02 = bytes(str_char01, encoding="utf-8")
str_char03 = create_string_buffer(100)
str_char03.raw = str_char02
print(f"申请空间后完整字符串的输出 is {str_char03.raw}")
print()
print(f"申请空间后字符输出 is {str_char03.value}")
print("============================\n")
so.str(byref(str_char03))

print(f"原字符串 is {str_char02}")
print(f"改变之后的字符串 is {str_char03.value}")
print(f"改变之后的字符串 is {repr(str_char03.value)}")
# 返回对象的规范字符串表示形式


print("=============================\n")
class Coordinate(Structure):
    _fields_ = [
        ('x', ctypes.c_int),
        ('y', ctypes.c_int)
    ]
coord = Coordinate(2, 3)
result = so.add_struct(coord)
print(f"结构体求和 {result}")


print("=============================\n")
class Point(Structure):
    _fields_ = [
        ('coordinate', Coordinate)
    ]
coord = Coordinate(100, 3)
pt = Point(coord)
result = so.add_struct_nesting(pt)
print(f"结构体嵌套求和 {result}")


print("=============================\n")
coord = Coordinate(21, 3)
result = so.struct_point(byref(coord))
print(f"结构体指针求和 {result}")